﻿#region Copyright

// /*
// SilverlightInEnterprise
// SilverlightInEnterprise.Caching.ServiceContractSL
// SalesOrderItem.cs
// 
// Author: Matt Van Horn
// Created 11/16/2011 10:57 AM
// Copyright: Silverlight in the Enterprise www.slinenterprise.com
// 
// Licensed under MS-PL http://www.opensource.org/licenses/MS-PL and while you 
// may use this in anyway you see fit and owe us nothing to do so, we wouldn't mind knowing that you got value out of the work we did (so we will do more) so 
// we ask that you leave a comment on www.slinenterprise.com or on the codeplex site slinenterprise.codeplex.com
// 
// */

#endregion

using System;
using System.ComponentModel;
using SilverlightInEnterprise.Caching.ServiceContractSL.DTOCache;
using SilverlightInEnterprise.Core.ViewModels;

namespace SilverlightInEnterprise.Caching.ServiceContract.DTO
{
    public partial class SalesOrderItem : ViewModelBase
    {
        private bool registeredNotification;


        private void OnValueChanged(object sender, PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "ProductId")
            {
                this.pullFromCache();
                this.OnPropertyChanged("Price");
                this.OnPropertyChanged("ProductName");
                this.OnPropertyChanged("IsTaxable");
                this.OnPropertyChanged("Total");
            }
        }

        private decimal? _price;

        public decimal? Price
        {
            get
            {
                if (!_price.HasValue)
                    this.pullFromCache();
                return _price;
            }
        }

        private bool? _isTaxable;

        public bool? IsTaxable
        {
            get
            {
                if (!_isTaxable.HasValue)
                    this.pullFromCache();
                return _isTaxable;
            }
        }

        private string _productName;

        public string ProductName
        {
            get
            {
                if (_productName == null)
                    this.pullFromCache();
                return this._productName;
            }
        }

        #region ActiveProductId Property

        private int _ActiveProductId;

        public int ActiveProductId
        {
            get
            {
                // if (this.ProductId != this._ActiveProductId)
                this._ActiveProductId = this.ProductId;
                return this._ActiveProductId;
            }
            set
            {
                if (this._ActiveProductId != value || this.ProductId != value)
                {
                    this.ProductId = value;
                    this._ActiveProductId = value;
                    this.OnPropertyChanged("ActiveProductId");
                    this.OnPropertyChanged("ProductId");
                }
            }
        }

        #endregion

        public decimal Total
        {
            get
            {
                decimal price = 0;
                if (this.Price.HasValue)
                    price = this.Price.Value;
                return Math.Round((decimal) this.Units*price, 2);
            }
        }

        private void pullFromCache()
        {
            if (!this.registeredNotification)
            {
                this.registeredNotification = true;
                this.PropertyChanged += OnValueChanged;
            }
            var product = ProductCache.Cache.GetItemByKey(this.ProductId);
            if (product == null)
            {
                this._isTaxable = null;
                this._productName = null;
                this._price = null;
            }
            this._isTaxable = product.IsTaxable;
            this._price = product.Price;
            this._productName = product.DisplayName;
        }
    }
}